<extend name="Layout/ins_page" />
<block name="content">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Form</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<link href="/sap/demo.css" rel="stylesheet" type="text/css" />
    <script src="/sap/scripts/boot.js" type="text/javascript"></script>
    

</head>
<body>


    <div id="form1" >
        <input name="id" class="mini-hidden"  value="{$data.id}"/>
        <table style="width: 700px; border-spacing:0px 10px;">
            
            <!-- <tr>
                <td>
                    <label for="textbox1$text">名称：</label>
                </td>
                <td>
                    <input id="textbox1"  name="name" class="mini-textbox" width='250px' required="true"  value ="{$data.name}"  />
                </td>
            </tr> -->
            
            
            <tr>
                <td>
                    <label for="textbox1$text">选择平台：</label>
                </td>
                <td>
    			<input id="btnEdit1" class="mini-buttonedit" onbuttonclick="onButtonEdit" style="width: 80%" text="{$data.cname}" value="{$data.code}"/>  
                </td>
            </tr>

               <tr>
                <td>
                    <label for="textbox1$text">选择供应商：</label>
                </td>
                <td>


                    <input   id="spl-select" class="mini-buttonedit" onbuttonclick="selectspl"   value="{$slp.id}" text="{$slp.name}" name="" textName="" style="width: 250px"/>
<!--onbuttonclick="selectspl" class="mini-buttonedit"  -->
                </td>
            </tr>
            
           
			 <tr>
                <td>
                    <label for="textbox1$text">品牌：</label>
                </td>
                <td>
                <input  label="ComboBox："   id="brand-select" class="mini-buttonedit" onbuttonclick="onButtonEdit2"   value="{$data.brand_id}" text="{$data.brand_name}" name="" textName="" style="width: 250px"/>
                   
                </td>
            </tr>
			
			 <!--<tr>-->
                <!--<td>-->
                    <!--<label for="textbox1$text">类目：</label>-->
                <!--</td>-->
                <!--<td>-->
                    <!--<input id="textbox1"  name="category" class="mini-textbox" required="true" width='250px'  value ="{$data.category}"/>-->
                <!--</td>-->
            <!--</tr>-->

            <tr>
                <td>
                    <label for="textbox1$text">类目(*必选,可多选)：</label>
                </td>
                <td>

                    <ul id="tree1" class="mini-tree" url="/SapSupplier/getitemcategorycodeOnlyLevelTwo" style="width:200px;padding:5px;"
                        showTreeIcon="true" textField="text" idField="id" parentField="pid" resultAsTree="false"
                        showCheckBox="true" checkRecursive="true"
                        onbeforenodecheck="onBeforeNodeCheck" allowSelect="false" enableHotTrack="false">
                    </ul>

                </td>
            </tr>

            <tr>
                <td>
                    <label for="textbox1$text">授权级别：</label>
                </td>
                <td>

                    <!--这个label一定要有-->
                    <label class="" for="type"></label>
                    <div class="">

                        <select class="form-control" name="auth_level" id="auth_level" style="width:150px">
                            <option value="1" <?php if($data['level']==1){ echo "selected='selected'";}?> >一级</option>
                            <option value="2" <?php if($data['level']==2){ echo "selected='selected'";}?> >二级</option>
                            <option value="3" <?php if($data['level']==3){ echo "selected='selected'";}?>>三级及其他</option>
                        </select>

                    </div>

                </td>
            </tr>
			
             <tr>
                <td>
                    <label for="textbox1$text">授权链：</label>
                </td>
                <td >
                <div id='sqlian' style="float: left">
                   <?php for($i=0;$i<$data['level'];$i++){
							   $auth_chain  = $data['auth_chain'][$i];
								if($i==$data['level']-1){
									echo "<input required='true' style='width:90px'  class='mini-textbox authinput' name='auth_chain[$i]' value='$auth_chain'>";
							   }else{
									echo "<input required='true' style='width:90px'  class='mini-textbox authinput' name='auth_chain[$i]' value='$auth_chain'>->";

									}
					}?>
					<?php if(count($data['level'])==0){
						echo "<input required='true' style='width:90px'  class='mini-textbox authinput' name='auth_chain[0]'>";
					}?>
					
					
					</div>
					 <a class="mini-button" iconCls="icon-add" style="background: none;border: none;float: left" id='addinput'>
                </td>
            </tr>
			
			
			 <tr>
                <td>
                    <label for="textbox1$text">授权有效期：</label>
                </td>
                <td>
                    <input id="start" name='start' required="true" class="mini-datepicker" value="{$data.start}" valueType='string'  />--<input valueType='string' required="true" id="end" name='end' class="mini-datepicker" value="{$data.end}"/>
                </td>
            </tr>
			
			
			 <tr>
                <td>
                    <label for="textbox1$text">备注：</label>
                </td>
                <td>
                    <input id="textbox1" required="true" name="note" class="mini-textarea" width='250px'  value ="{$data.note}"/>
                </td>
            </tr>

            <tr>
                <td>
                    <label for="textbox1$text">推荐人：</label>
                </td>
                <td>
                    <input id="textbox1" id="recommender"  name="recommender" class="mini-textbox" emptyText="如实填写" required="true" style="width:350px"  value ="{$data.recommender}"/>
                </td>
            </tr>
            
             <tr>
                <td>
                    <label for="textbox1$text">附件(*商标注册证、<br>转让及续展证明、<br>完整授权链之授权书)：</label>
                </td>
                <td>
	                 <div class="uploader" id="uploader_add_file" add_file_id="add_file">
					 <input id="add_file_add_file" class="add_file"  name="attachment" value="{$data.attachment}" style="display: none" >
					 <a id="btn_add_file" href="javascript:;" class="">添加附件</a>
					<ul class="file_list" new_upload="" >
				<?php foreach($data['att'] as $att){?>
					<li class="tbody" id="{$att.md5}" add_file="{$att.code}">
					<div class="loading" style="width: 100%;"></div>
					<div class="data" >
					<span class="del text-center" style='width: 75px'>

                        <if condition="$att.cancelled == 1">

                                            <b style="color: red">已作废</b>

                                            <else/>

                                            <span id="cancelleddoc" name="cancelleddoc">
                                                <a href="javascript:canceldoc({$att.id})">作废</a>
                                            </span>

                                        </if>

                        <a class="link showpic" data='/Uploads/Download/{$att.savepath}{$att.savename}'>查看</a></span>
                        <!--<a class="link del" style="display: inline;">删除</a>-->
					<span class="size text-right">{$att.size} kb</span><span class="auto autocut">{$att.name}</span></div>
					</li>
				<?php }?>
					
					</ul>
				</div> 
                </td>
            </tr>

            <tr>
                <td>
                    审批备注:
                </td>
                <td>
                    <input style="width:400px;" name="remarkes" id='remarkes' class="mini-textarea" required="true"/>
                </td>
            </tr>
            
			<tr>
                <td>
                    
                </td>
                <td>
				    <br/>
                    <a class='mini-button' onclick="submitForm(1)" >保存</a>
                    <a class='mini-button' onclick="" >取消</a>
                    <!--CloseWindow('cancel')-->
                                                          
                    
                </td>
            </tr>

            <if condition=' ($haspermission) '>


            <tr>
                <td colspan="2" align="center">
                    <a class="mini-button" onclick="review(1)" id='query-bt'
                       style="background-color: #81de81;color: white;width: 100px;height: 30px;line-height: 30px;font-weight: bold;">&nbsp;&nbsp;通过&nbsp;&nbsp;</a>
                    <a class="mini-button" onclick="review(-1)" id='query-bt'
                       style="background-color: #de4c2b;color: white;width: 100px;height: 30px;line-height: 30px;font-weight: bold;">&nbsp;&nbsp;不通过&nbsp;&nbsp;</a>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="left">
                    <span>操作记录</span>
                    <div id='acc_log'>
                        <ul id='acc-log-ul'></ul>
                    </div>
                </td>
            </tr>

            </if>
            
        </table>
    </div>
    
    
    
    <script type="text/javascript">
        mini.parse();

        // tree beginning
        var tree = mini.get("tree1");
        var nodes = tree.getAllChildNodes(tree.getRootNode());
        tree.uncheckNodes(nodes);

        <?php
            echo 'tree.setValue("'.$data['category'].'");';
        echo 'tree.expandPath("'.$firstCategory.'");';
            ?>
        // tree ending

        function onButtonEdit(e) {
            var btnEdit = this;

            mini.open({
                url:  "/auth/select",                          
                title: "选择平台",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                  
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);
                        btnEdit.setValue(data.id);
                        btnEdit.setText(data.text);
                    }
                }
            });            
             
        }  
        
        
        function selectspl(e) {
            var btnEdit = this;
            mini.open({
                url:  "/index.php?m=&c=SapSupplier&a=supplierselect",
                title: "选择供应商",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    if (action == "ok") {
                    	 var iframe = this.getIFrameEl();
                         var data = iframe.contentWindow.GetData();
                         data = mini.clone(data);    //必须
                         if (data) {
                        	   btnEdit.setValue(data.id);
                               btnEdit.setText(data.name);
                              
                         }
                    }

                }
            });

        }
        
    	 function onValueChanged(e) {
             
             var item = e.selected;
             if (item) { 
                 mini.get('cname-show').setValue(item.text);  
             }
         }
    
		jQuery('.showpic').click(function(){
			window.open(jQuery(this).attr('data'));
			
		})
      

        function submitForm(tag) {
			
			var code = mini.get('btnEdit1').getValue();
			var name = mini.get('btnEdit1').getText();
			
            //提交表单数据
            var form = new mini.Form("#form1");            
            var data = form.getData();      //获取表单多个控件的数据
            data.attachment = jQuery('#add_file_add_file').val();
            data.brand_code = mini.get('brand-select').getValue();
            data.brand_name = mini.get('brand-select').getText();
            data.spl_id = mini.get('spl-select').getValue();
            data.spl_name = mini.get('spl-select').getText();
            data.code = code;
            data.cname = name;

            data.level = $("#auth_level").val();

            // get tree value
            var tree = mini.get("tree1");
            var treeValue = tree.getValue(false);
            var treeName = tree.getText(false);
            //alert(treeValue);
            data.category = treeValue;
            data.categorytxt = treeName;

            // check empty
            if(treeValue) {}
            else {
                mini.alert("类目必选！");
                return;
            }

            form.validate();
            if (form.isValid() == false) return;

            if ($(".uploader .tbody").length > 0)  {
            }
            else {
                mini.alert("必须上传相关附件！");
                return;
            }

            var json = mini.encode(data);   //序列化成JSON
			
           $.ajax({
                url: "/auth/dosave",
                type: "post",
                data: { data: json },
                success: function (text) {
					if(text>0){
						if(tag==0){
							//CloseWindow('ok');
						}else{
							mini.alert('保存成功');
						}
						
					}else{
						mini.alert('保存失败...');
					}
                   
                }
            });
           //CloseWindow("ok");
        }
     
		  function CloseWindow(action) {
		        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
		        else window.close();
		    }

   
   
   jQuery('#addinput').click(function(){
	   
	   var authinputlist = jQuery('.authinput');
	   var count =authinputlist.length;

	  var   html ="-><input required='true' style='width:90px'  class='mini-textbox authinput' name='auth_chain["+count+"]'>";
	   
	   jQuery('#sqlian').append(html); mini.parse();
	  
	   });
	  
  
   
   function onButtonEdit2(e) {
       var btnEdit = this;
       mini.open({
           url:  "/index.php?m=&c=SapSupplier&a=brandselect",
           title: "选择品牌",
           width: 650,
           height: 380,
           ondestroy: function (action) {
               if (action == "ok") {
               	 var iframe = this.getIFrameEl();
                    var data = iframe.contentWindow.GetData();
                    data = mini.clone(data);    //必须
                    if (data) {
                   	   btnEdit.setValue(data.b_id);
                        btnEdit.setText(data.b_name);
                    }
               }

           }
       });

   }


    </script>
    
    <script type="text/javascript">
    
    $(document).ready(function() {
    	var uploaders = {};
    	$('.uploader').each(function() {
    		var uploader, $target, $id;
    		$target = $(this);
    		$id = $target.attr('add_file_id');
    		$btn_id = 'btn_' + $id;
    		$uploader_id = 'uploader_' + $id;

    		settings = {
    			runtimes : 'html5,flash',
    			browse_button : $btn_id, // you can pass in id...
    			container : document.getElementById($uploader_id),
    			url : upload_url,
    			flash_swf_url : app_path + '/Public/Static/plupload/Moxie.swf',
    			filters : {
    				max_file_size : '1000mb'
    			}
    		};

    		uploader = new plupload.Uploader(settings);
    		uploaders[$id] = uploader;

    		function FilesAdded(up, files) {
    			
    			for (var i in files) {
    				var str = JSON.stringify(files);
    			
    				if( files[i].id!=null){
    				 
    				html = '<li class="tbody" id="' + files[i].id + '">\n';
    				
    				html += '<div class="loading"></div>\n';
    				html += '<div class="data">\n';
    				
    				html += '<span class="del text-center"><a class="link del">删除</a></span>\n';
    				
    				html += '<span class="size text-right">' + plupload.formatSize(files[i].size) + '</span>';
    				html += '<span class="auto autocut">' + files[i].name + '</span>';
    				html += '</li>';
    				html += '</div>\n';
    				
    				$('#uploader_' + $id + ' .file_list').append(html);
    				}
    			}
    			up.start();
    		}

    		uploader.bind("PostInit", function(up) {
    			if ($(".uploader .tbody").length > 0) {
    				$(".uploader .tbody .loading").css("width", "100%");
    				$(".uploader .thead").show();
    				$(".uploader .tbody").each(function() {
    					id = $(this).attr("filename");
    					filename = $(this).attr("filename");
    					size = $(this).attr("size");
    					file = new plupload.File(id, filename, size);
    					file.status = plupload.DONE;
    					count = uploader.files.length;
    					uploader.files[count] = file;
    					path = 'hhhhhhh';
    				});
    			}
    		});

    		uploader.init();

    		uploader.bind('FilesAdded', FilesAdded);

    		uploader.bind("UploadProgress", function(up, file) {
    			$("#" + file.id).find("a.del").hide();
    			$("#" + file.id).find('.loading').css("width", file.percent + "%");
    		});

    		uploader.bind('FileUploaded', function(up, file, data) {
    			var myObject = eval('(' + data.response + ')');
    			if (myObject.status) {
    				if ($("#add_file_" + $id).length != 0) {
    					$("#add_file_" + $id).val($("#add_file_" + $id).val() + myObject.sid + ";");
    				}
    				$("#" + file.id).attr("add_file", myObject.sid);

    				$new_upload = $("#uploader_" + $id + " .file_list").attr("new_upload");
    				$("#uploader_" + $id + " .file_list").attr("new_upload", $new_upload + myObject.sid + ";");

    				$("#" + file.id).find("a.del").show();
    			} else {
    				ui_alert(myObject.info, function() {
    					$("#" + file.id).remove();
    				});
    			}
    		});
    	});
    });

    window.onbeforeunload = function(e) {
    	e = e || window.event;
    	// For IE and Firefox prior to version 4
    	$new_upload = $(".file_list").attr("new_upload");
    	if ($new_upload !== undefined) {
    		if ($new_upload.length) {
    			if (e) {
    				e.returnValue = '上传的附件将被删除，确定退出吗？';
    			}
    			// For Safari
    			return '上传的附件将被删除，确定退出吗？';
    		}
    	}
    };

    $(document).on("click", ".uploader a.del", function() {
    	$obj = $(this).parents("li");
    	$uploader = $(this).parents('.uploader');
    	ui_confirm("确定要删除吗？", function() {

    		$current_del_file = $obj.attr("add_file");
    		$(".add_file", $uploader).val($(".add_file", $uploader).val().replace($current_del_file + ";", ""));

    		$new_upload = $(".file_list", $uploader).attr("new_upload");
    		$(".file_list", $uploader).attr("new_upload", $new_upload.replace($current_del_file + ";", ""));

    		$obj.remove();
    	});
    });

    function onBeforeNodeCheck(e) {
        var tree = e.sender;
        var node = e.node;
        if (tree.hasChildren(node)) {
            //e.cancel = true;
        }
    }

    </script>

    <script type="text/javascript">

        // 检测备注
        function checkRemarkes() {
            var remarkes = mini.get('remarkes').getValue();
            if (remarkes) {
                return true;
            } else {
                return false;
            }
        }

        // 审批操作
        function review(status) {

            if (checkRemarkes()){}
            else{
                mini.alert("备注必填！");
                return;
            }

            doreview(status);

        }
        function doreview(status) {

            mini.mask({
                el: document.body,
                cls: 'mini-mask-loading',
                html: '保存中...'
            });

            var remarkes = mini.get('remarkes').getValue();
            $.ajax({
                url: "/Auth/review20170706",
                data: {id: <?php echo $data['id'];?>, status: status, remarkes: remarkes},
            type: "post",
                success: function (data) {
                if (data == 1) {
                    mini.unmask(document.body);
                    //mini.alert('保存成功!');
                    getlog(<?php echo $data['id'];?>);
                    return;
                } else {
                    mini.unmask(document.body);
                    mini.alert(data.msg);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
        }

        // 获取记录
        function getlog(id) {
            $.ajax({
                url: '/Auth/getlog',
                data: {id: id},
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    var loghtml = "";
                    for (var i = 0; i < data.length; i++) {
                        loghtml += "<li>用户:" + data[i].user + ' 审核动作:' + data[i].info + " 时间：" + data[i].ctime + "</li>";
                    }
                    $("#acc-log-ul").html(loghtml);
                }

            })
        }

        getlog(<?php echo $data['id'];?>);

        function canceldoc(id) {

            //确认提示
            mini.confirm("确定作废文件？", "确定？",
                function (action) {
                    if (action == "ok") {

                        $.ajax({
                            url: "/SapSupplier/canceldoc",
                            type: "post",
                            data: {id: id},
                            dataType: 'json',
                            success: function (data) {
                                mini.alert(data.msg);
                                //var cancelleddoc = mini.get('cancelleddoc');
                                //cancelleddoc.html('abc');
                                //$("#cancelleddoc").html("<b style=\"color: red\">已作废</b>");
                            }
                        });


                    } else {

                    }
                }
            );

        }

    </script>

</body>
</html>

</block>
<block name="js">
	<script type="text/javascript">
		function save() {
			sendForm("form_data", "{:U('save')}", "{:U('index')}");
		}
	</script>
</block>